from fastapi import FastAPI
import subprocess
import os

app = FastAPI()

# 定义固定的Python脚本名称
fixed_python_script_name = "Clock.py"


@app.post("/run_fixed_python_script/")
async def run_fixed_python_script():
    try:
        # 获取当前工作目录
        current_dir = os.getcwd()

        # 使用相对路径执行Python脚本
        result = subprocess.run(
            ["python", fixed_python_script_name],
            check=True,
            cwd=current_dir  # 设置工作目录为当前目录
        )
        return {"status": "success", "message": "Python脚本执行成功"}
    except subprocess.CalledProcessError as e:
        return {"status": "error", "message": f"执行Python脚本时出错: {e}"}
    except FileNotFoundError:
        return {"status": "error", "message": "指定的Python脚本不存在"}


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8004)
